<?php

header('Content-Type: text/html; charset=UTF-8');

class Calculator
{
	private 
	$value = 0;
	
	private 
	$bufer = 0;
	
	private 
	$action = null; // sqrt, add, sub, mul, div, percent
	
	private 
	$memory = 0;
	
	private
	function calculate()
	{
		switch ($this->action) 
		{
			case 'sqrt':
				$this->value = sqrt($this->bufer);
				break;
				
			case 'add':
				$this->value += $this->bufer;
				break;
				
			case 'sub':
				$this->value = $this->bufer - $this->value;
				break;
				
			case 'mul':
				$this->value *= $this->bufer;
				break;
			
			case 'div':
				$this->value = $this->bufer / $this->value;
				break;
			
			case 'percent':
				$this->value = $this->bufer * $this->value / 100;
				break;
			
		}
		$this->bufer = 0; 
		$this->action = null; 
	}
	
	function action($action, $value)
	{
		if (isset($value)) 
		{
			$this->value = $value;
		}
		switch ($action) 
		{
			case 'sqrt':
				$this->action = 'sqrt';
				$this->bufer = $this->value;
				$this->calculate();
				break;
				
			case 'percent':
				$this->action = 'percent';
				$this->bufer = $this->value;
				break;
				
			//---------------------------------------			
			case 'memoryCencel':
				$this->memory = 0;
				break;
			
			case 'memoryRead':
				$this->value = $this->memory;
				break;
			
			case 'subFromMemory':
				$this->memory -= $this->value;
				break;
			
			case 'addToMemory':
				$this->memory += $this->value;
				break;
			
			case 'memoryU':
				break;
			//---------------------------------------				
			case 'assign':
				$this->calculate();
				break;
			//---------------------------------------
			case 'oprMultiply':
				$this->action = 'mul';
				$this->bufer = $this->value;
				break;
				
			case 'oprDivide':
				$this->action = 'div';
				$this->bufer = $this->value;
				break;
				
			case 'oprSubstraction':
				$this->action = 'sub';
				$this->bufer = $this->value;
				break;
				
			case 'oprAdd':
				$this->action = 'add';
				$this->bufer = $this->value;
				break;
			//---------------------------------------								
			case 'close':
				$this->action = null;
				$this->memory = 0;
				$this->bufer = 0;
				$this->value = 0;
				break;
				
			case 'AC':
				break;
				
		} 
	}
	
	function
	getValue()
	{
		return $this->value;
	}
}

@session_start();
$o = null;
if ($_SESSION['o']) {
	$o = unserialize($_SESSION['o']);
} else {
	$o = new Calculator();
}

if ($_REQUEST['action']) {
	$o->action($_REQUEST['action'], @$_REQUEST['data']);
}

define('DS', DIRECTORY_SEPARATOR);
$DOCRoot = str_replace('/', DS, str_replace('\\', DS, $_SERVER['DOCUMENT_ROOT']));
$DOCRoot = strrpos($DOCRoot, DS) !== (strlen($DOCRoot) - strlen(DS)) ? $DOCRoot . DS : $DOCRoot;

function
text($text)
{
	return htmlspecialchars($text, ENT_NOQUOTES);
}

function attr($text)
{
	return htmlspecialchars($text, ENT_COMPAT);
}

function
path2url($text)
{
	global $DOCRoot;
	if (strpos($text, $DOCRoot) !== 0) {
		throw new Exception();
	}
	
	return 'http://'.$_SERVER['HTTP_HOST'].'/'.str_replace(DS, '/', substr($text, strlen($DOCRoot)));
}

$headScripts = array(
	array(
		'URL' => 'http://code.jquery.com/jquery-1.4.1.min.js',
		'Headers' => array(
			'ContentType' => 'text/javascript; charset=UTF-8',
		),
	)
);

$styles = array(
	array(
		'URL' => path2url(dirname(__FILE__).DS.'css'.DS.'main.css'),
		'Headers' => array(
			'ContentType' => 'text/css; charset=UTF-8',
		),
	)
);

$view = basename(__FILE__);

$pageTitle = 'Calculator';

include dirname(__FILE__).DS.'template.php';

$_SESSION['o'] = serialize($o);


?>